const Cesium = window.Cesium;
import { COLOR } from '../../Config';

const OPTIMIZE_OFFSET = 1000;
class TrailLine {
    constructor(patent) {
        this.positions = [];

        this.NextPosition = patent.BaseEntity.CesiumPosition;
        this.ProPosition = patent.BaseEntity.CesiumPosition;

        var _update = () => {
            if (patent.BaseEntity.CesiumPosition && this.ProPosition) {
                return [this.ProPosition, patent.BaseEntity.CesiumPosition];
            } else {
                return [];
            }
        }

        this.MoveEntity = new Cesium.Entity({
            show: false,
            polyline: {
                positions: new Cesium.CallbackProperty(_update, false),
                material: new Cesium.PolylineDashMaterialProperty({
                    color: COLOR.DEFAULT,
                    // gapColor: Cesium.Color.TRANSPARENT,
                    // dashLength: 16,
                }),
                // 线宽
                width: 3,
                // 贴地
                clampToGround: true,
            },
        });

        var _EntityPositionsUpdate = () => {
            return this.positions;
        };

        this.Entity = new Cesium.Entity({
            show: false,
            polyline: {
                positions: new Cesium.CallbackProperty(_EntityPositionsUpdate, false),
                material: new Cesium.PolylineDashMaterialProperty({
                    color: COLOR.DEFAULT,
                    gapColor: Cesium.Color.TRANSPARENT,
                    dashLength: 16,
                }),
                // 线宽
                width: 3,
                // 贴地
                clampToGround: true,
            },
        });
    }
    _pointToLine(p1, p2, pt) {
        let distance = 0;
        let a, b, c;
        a = Cesium.Cartesian3.distance(p1, p2); // 线段的长
        b = Cesium.Cartesian3.distance(p1, pt); // 到点的距
        c = Cesium.Cartesian3.distance(p2, pt); // 到点的距
        if (c <= 1 || b <= 1) {
            // distance = 0;
            return false;
        }
        if (a <= 1 && b <= 1) {
            // distance = b;
            return true;
        }
        if (a <= 1 && b >= OPTIMIZE_OFFSET) {
            // distance = b;
            return false;
        }
        let p = (a + b + c) / 2; // 半周�
        let s = Math.sqrt(p * (p - a) * (p - b) * (p - c)); // 海伦公式求面
        distance = 2 * s / a; // 返回点到线的距离（利用三角形面积公式求高
        return distance < OPTIMIZE_OFFSET;
    }
    _optimizetrailLine(C, _array) {
        var _length = _array.length;
        var A = _array[_length - 2];
        var B = _array[_length - 1];
        var ans = this._pointToLine(A, B, C);
        if (ans) {
            _array.splice(_length - 1, 1, C);
        } else {
            _array.push(C);
        }
        return _array;
    }
    setPotions(position) {
        if (this.positions.length < 2) {
            this.positions.push(this.NextPosition);
        } else {
            this._optimizetrailLine(this.NextPosition, this.positions);
        }
        this.ProPosition = this.NextPosition;
        this.NextPosition = position;
    }
    show() {
        this.Entity.show = true;
        this.MoveEntity.show = true;
    }
    hide() {
        this.Entity.show = false;
        this.MoveEntity.show = false;
    }
}



export { TrailLine };
